//
//  BaseViewController.swift
//  liaoningdangdai
//
//  Created by 邵禹曦 on 2019/11/26.
//  Copyright © 2019 白雨桐. All rights reserved.
//

import UIKit
import SwiftyJSON


/// 父类
class BaseViewController: UIViewController
{
    
    lazy var landView : LandViewController = LandViewController()
    // MARK:- 定义变量
    var isLogin : Bool = true
    // 未登陆
    let noLogin : String? = "-102130040002"
    let noLogin4004 :String = "-102130040004"
     /*
    override func loadView() {
        
       
        /// 1、获得沙盒的根路径
        let home = NSHomeDirectory() as NSString
        /// 2、获得Documents路径，使用NSString对象的stringByAppendingPathComponent()方法拼接路径
        let docPath = home.appendingPathComponent("Documents") as NSString
        /// 3、获取文本文件路径
        let filePath = docPath.appendingPathComponent("data.plist")
        
        // 读取信息
        
        //let account = NSArray(contentsOfFile: filePath)
        let account = NSKeyedUnarchiver.unarchiveObject(withFile: filePath)
        
    }*/
    

    override func viewDidLoad() {
        super.viewDidLoad()

    }
    
    
    
}

// MARK: - 自定义方法
extension BaseViewController {
    
    // 归档
    func saveWithFile(a : Int, u : String, retcode : String)
    {
           // 1、获得沙盒的根路径
           let home = NSHomeDirectory() as NSString
           // 2、获得Documents路径，使用NSString对象的stringByAppendingPathComponent()方法拼接路径
           let docPath = home.appendingPathComponent("Documents") as NSString
           // 3、获取文本文件路径
           let filePath = docPath.appendingPathComponent("data.plist")
           let dataSource = NSMutableArray()
            dataSource.add(a)
            dataSource.add(u)
            dataSource.add(retcode)
           
           //print(dataSource)
           // 4、将数据写入文件中
           dataSource.write(toFile: filePath, atomically: true)
           print(filePath)
       }
       
    // 解档
    func readWithFile(a: Int)->(Any)
    {
           /// 1、获得沙盒的根路径
           let home = NSHomeDirectory() as NSString
           /// 2、获得Documents路径，使用NSString对象的stringByAppendingPathComponent()方法拼接路径
           let docPath = home.appendingPathComponent("Documents") as NSString
           /// 3、获取文本文件路径
           let filePath = docPath.appendingPathComponent("data.plist")
           //let dataSource = NSArray(contentsOfFile: filePath)
           //let dataPath = NSArray(arrayLiteral: filePath)
           let dataSource = NSArray(contentsOfFile: filePath)
           
           //print(dataSource as Any)
           //print(dataPath)
            return dataSource![a] as Any
       }
    
    
    
}
   


// MARK: - 一些零碎的东西
extension BaseViewController {
    
    /// 跳转
    @objc func goBack(identifier: String)
    {
        let login = UIStoryboard.init(name: "Main", bundle: nil)
        let play = login.instantiateViewController(identifier: identifier)
                 
        // self.navigationController?.pushViewController(play, animated: true)
        self.present(play, animated: true, completion: nil)
    }
    
    // 请登陆
    @objc func pLogin(result : [String : AnyObject]) -> Bool
    {
        // 解包
        let json = JSON(result)
        // 如果接口请求成功但是登陆失败U是空的
        let retcode : String? = json["REP"]["RESULT"]["RETCODE"].stringValue
        
        if (self.noLogin == retcode)
        {
            print("请登陆")
            self.goBack(identifier: "Login")
            return false
        } else if (self.noLogin4004 == retcode)
        {
            print("请登陆")
            self.goBack(identifier: "Login")
            return false
        }
        else {
            return true
        }
    }
}
